<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">报文数据列表</h4>
</div>
<div class="modal-body" style="overflow:auto; height: 500px">
    <input type="hidden" th:value="${id}" id="id">
    <div id="toolbar1" class="btn-group btn-group-sm" style="float: right">
        <a type="button" class="btn btn-success" data-toggle="modal"
           th:href="@{'/project/objectManage/message_edit?equipment_id='+${id}}" data-target="#modal3">新增</a>
        <a type="button" class="btn btn-danger"
           onclick="delDatas('/project/objectManage/message_delete', 'dataTable1')">删除</a>
        <a type="button" class="btn btn-info" onclick="message_edit()">编辑</a>
        <a type="button" th:href="@{'/project/objectManage/message_export?id='+ ${id}}"
           class="btn btn-success">导出配置文件</a>
        <a type="button" onclick="fileUpload()" class="btn btn-success">导入配置文件</a>
        <input type='file' id='file_upload' onchange='saveConfige()' style='display: none'/>
    </div>
    <table id="dataTable1" data-url="/project/objectManage/message_data" data-page-list=[] data-striped="true"
           data-pagination="true" data-page-size="10" data-cache="false" data-toolbar-align="right">
    </table>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
</div>
<!--数据表-->
<script>
    var dataFormat_message = [{
        checkbox: true
    }, {
        field: 'id',
        title: 'id',
        visible: false
    }, {
        field: 'subjectName',
        title: '科目名称'
    }, {
        field: 'canId',
        title: 'CAN_ID'
    }, {
        field: 'canLong',
        title: 'CAN数据长度'
    }, {
        field: 'head',
        title: '大小头'
    }, {
        field: 'dataType',
        title: '数据类型'
    }, {
        field: 'beginByte',
        title: '起始字节'
    }, {
        field: 'beginPosition',
        title: '起始位'
    }, {
        field: 'endByte',
        title: '结束字节'
    }, {
        field: 'endPosition',
        title: '结束位'
    }, {
        field: 'offset',
        title: '偏移量'
    }, {
        field: 'multiple',
        title: '倍率'
    }, {
        field: 'unit',
        title: '单位'
    }, {
        field: 'objId',
        title: '对应平台编号'
    }];

    function init_list_table(){
        $("#dataTable1").bootstrapTable({
            method: "get",
            sidePagination: "server",
            dataType: "json",
            toolbar: '#toolbar1',
            queryParams: function queryParams(params) {   //设置查询参数
                var param = {
                    id: $("#id").val(),
                    pageNum: params.offset / params.limit
                };
                $(".search-data").each(function () {
                    var key = $(this).attr("name");
                    var val = $(this).val();
                    if (val) {
                        param[key] = val;
                    }
                });
                return param;
            },
            columns: dataFormat_message
        });
    }

    $(function () {
        init_list_table();
    });
</script>
<!--页面上的js-->
<script>
    /**
     * 打开编辑页面
     */
    function message_edit() {
        var table = $("#dataTable1").bootstrapTable('getSelections');
        if (table.length == 0) {
            alert("请选择一条记录");
            return false;
        } else if (table.length > 1) {
            alert("只能选择一条记录");
            return false;
        }
        var result = table[0].id;
        $("#modal3").modal({
            remote: '/project/objectManage/message_edit?equipment_id=' + $("#id").val() + "&id=" + result
        });
    }

    /**
     * 点击上传就直接打开选择文件页面
     */
    function fileUpload() {
        $("#file_upload").trigger("click");
    }

    /**
     * 导入配置文件
     */
    function saveConfige() {
        var fd = new FormData();
        fd.append("equipment_id", $("#id").val());
        fd.append("file", $("#file_upload")[0].files[0]);
        $.ajax({
            url: "message_import",
            type: "POST",
            processData: false,
            contentType: false,
            data: fd,
            success: function (result) {
                alert(result.message);
                $("#file_upload").val(null);
                $("#dataTable1").bootstrapTable("selectPage",1);
            }
        });
    }
</script>

